import { Component } from '@angular/core';
import {App,IonicPage, NavController, NavParams, ModalController} from 'ionic-angular';
import {ComplaintApiService} from "../../api/complaint-api";
import {TopTabModel} from "../../model/topTab.model";
import {SugesstionConfirmPage} from "../sugesstion-confirm/sugesstion-confirm";
import {PageParam} from "../../api/api.service";
import {EventService} from "../../service/event.service";

/**
 * Generated class for the SugesstionPage page.
 *
 * See http://ionicframework.com/docs/components/#navigation for more info
 * on Ionic pages and navigation.
 */
@IonicPage()
@Component({
  selector: 'page-sugesstion',
  templateUrl: 'sugesstion.html',
})
export class SugesstionPage {
  pageParam:PageParam;
  hasMore:boolean = true;

  items:Array<TopTabModel> = [];
  currentPageIndex:number = 0;
  data:any={
    category:'',
    content:''
  };

  constructor(public navCtrl: NavController,
              public navParams: NavParams,
              private modalCtrl: ModalController,
              public app:App,
              public eventService:EventService,
              private complaintApi:ComplaintApiService) {
    this.items.push(new TopTabModel({id:0,name:'我要投诉',isSelect:true,func:this.tabChange}));
    this.items.push(new TopTabModel({id:1,name:'我的投诉',func:this.tabChange}));
  }

  ionViewDidLoad() {
    this.loadCategory();
    this.doRefresh();
    this.eventService.subscribe(EventService.REFRESH_USER, ()=> {
      this.doRefresh()
    })
  }

  tabChange = v=>{
    for(let item of this.items){
      item.isSelect=false
    };
    v.isSelect = true;
    this.currentPageIndex = v.id;
  }

  complaintCategory:any=[];

  complaintDetailList:any=[];

  statusStyle(item:any){
    let fontColor:any = item.status==1?"orange":"#90c31f";
    return {
      "color":fontColor
    }
  }
  ionViewDidEnter() {
    document.title='投诉建议';
  }
  loadCategory(){
    this.complaintApi.getComplaintCategory({},(res:any)=>{
      console.log(res);
      let list:any = [];
      for(let item of res){
        let data:any={
          code:item.code,
          name:item.name
        }
        list.push(data);
      }
      this.complaintCategory = list;
    })
  }

  sumbitComplaint(data:any){
    let reqData:any={
      category:data.category,
      content:data.content,
    };
    console.log(reqData);
    this.complaintApi.postComplaint(reqData,(res:any)=>{
      console.log(res);
      let profileModal = this.modalCtrl.create(SugesstionConfirmPage, this.data, {showBackdrop: true});
      profileModal.present();
    })
  }

  doRefresh(refresher?) {
    this.pageParam = new PageParam()
    this.complaintApi.getComplaint(v => {
      this.complaintDetailList = v.content
      console.log(this.complaintDetailList);
      this.hasMore = !v.last;
      if (refresher) {
        refresher.complete();
      }
    }, this.pageParam);
  }

  get canLoadMore():boolean {
    return this.hasMore;
  }

  doInfinite(infiniteScroll) {
    ++this.pageParam.page;
    this.complaintApi.getComplaint(v => {
      this.hasMore = !v.last;
      this.complaintDetailList = this.complaintDetailList.concat(v.content);
      infiniteScroll.complete();
    }, this.pageParam);
  }
}
